package com.rzico.core.plugin;

import com.rzico.core.entity.SysPlugin;
import com.rzico.core.model.PluginAttribute;
import com.rzico.core.plugin.dada.dadaVo.DadaApiResponse;
import com.rzico.core.rider.RiderApiRequest;
import com.rzico.core.rider.RiderApiResponse;
import com.rzico.core.utils.HttpClientUtil;
import com.rzico.core.utils.JSONUtil;
import org.apache.commons.lang3.builder.CompareToBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Plugin - Dada配送插件
 */
public abstract class RiderPlugin implements Comparable<RiderPlugin> {

    @Value("${setting.host}")
    private String host;

    public String getHost() {
        return host;
    }
    /**
     * 获取ID
     *
     * @return ID
     */
    public final String getId() {
        return getClass().getAnnotation(Component.class).value();
    }

    /**
     * 获取名称
     *
     * @return 名称
     */
    public abstract String getName();

    /**
     * 获取版本
     *
     * @return 版本
     */
    public abstract String getVersion();

    public List<PluginAttribute> getAttributeKeys() {
        List<PluginAttribute> data = new ArrayList<>();
        data.add(new PluginAttribute("app_key","appKey",PluginAttribute.STRING_VALUE));
        data.add(new PluginAttribute("app_secret","appSecret",PluginAttribute.STRING_VALUE));
        data.add(new PluginAttribute("source_id","sourceId",PluginAttribute.STRING_VALUE));
        return data;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        MsgPlugin other = (MsgPlugin) obj;
        return new EqualsBuilder().append(getId(), other.getId()).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37).append(getId()).toHashCode();
    }

    @Override
    public int compareTo(RiderPlugin riderPlugin) {
        return new CompareToBuilder().append(getId(), riderPlugin.getId()).toComparison();
    }

    /**
     * 提交订单
     */
    public abstract RiderApiResponse addOrder(SysPlugin sysPlugin, RiderApiRequest orderAddRequest);

    /**
     * 计算运费
     */
    public  abstract RiderApiResponse calculateFee(SysPlugin sysPlugin, RiderApiRequest orderAddRequest);

    /**
     * 查询订单详情   可跟踪骑手经纬度
     * @param orderSn
     * @return
     */
    public abstract RiderApiResponse query(SysPlugin sysPlugin, String orderSn);

    /**
     * 订单取消
     * @param  orderSn 订单号，cancelReason 取消原因 , cancelReasonId 取消原因ID
     *            1 没有配送员接单
                  2 配送员没来取货
                  3 配送员态度太差
                  4 顾客取消订单
                  5 订单填写错误
                 34 配送员让我取消此单
                 35 配送员不愿上门取货
                 36 我不需要配送了
                 37 配送员以各种理由表示无法完成订单
     * @return
     */
    public abstract RiderApiResponse cancel(SysPlugin sysPlugin, String orderSn,String cancelReason,Integer cancelReasonId);

    /**
     * 异常通知检查
     */
    public abstract RiderApiResponse verifyNotify(SysPlugin sysPlugin, HttpServletRequest request);


    /**
     * 获取用户号
     * @param sysPlugin SysPlugin
     * @return 获取用户号
     */
    public String getNotifyUrl(SysPlugin sysPlugin) {
        return host+"/rider/notify/"+getId()+"/"+sysPlugin.getMchId();
    }


    /**
     * 获取请求地址
     */
    public String getRequestUrl(SysPlugin sysPlugin,String requestUrl){
        return host+requestUrl;
    }


    /**
     * 获取通知返回消息
     * @param request httpServletRequest
     * @return ;
     */
    public abstract String getNotifyMessage(HttpServletRequest request);

}
